/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.DataStructures
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using Generic.Patterns;

    [Serializable]
    [ComVisible(true)]
    [DebuggerDisplay("Count = {Count}")]
    public abstract class CollectionBase<T> : ICollection<T>, ICloneable
    {
        #region Constructors
        List<T> _list;
        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionBase&lt;T&gt;"/> class.
        /// </summary>
        protected CollectionBase()
        {
            _list = new List<T>();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionBase&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="capacity">The capacity.</param>
        protected CollectionBase(int capacity)
        {
            _list = new List<T>(capacity);
        }
        #endregion

        #region private List<T> InnerList
        private List<T> InnerList
        {
            get
            {
                if (_list == null)
                    _list = new List<T>();
                return _list;
            }
        }
        #endregion

        #region Protected

        protected virtual void OnSet(int index, T oldValue, T newValue)
        {
        }

        protected virtual void OnInsert(int index, T value)
        {
        }

        protected virtual void OnClear()
        {
        }

        protected virtual void OnRemove(int index, T value)
        {
        }

        protected virtual void OnValidate(T value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
        }

        protected virtual void OnSetComplete(int index, T oldValue, T newValue)
        {
        }

        protected virtual void OnInsertComplete(int index, T value)
        {
        }

        protected virtual void OnClearComplete()
        {
        }

        protected virtual void OnRemoveComplete(int index, T value)
        {
        }

        #endregion

        #region ICollection<T> Members

        #region public int Count
        /// <summary>
        /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <value></value>
        /// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</returns>
        public int Count
        {
            get { return InnerList.Count; }
        }
        #endregion

        #region public void Add(T item)
        /// <summary>
        /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
        public void Add(T item)
        {
            OnValidate(item);
            OnInsert(InnerList.Count, item);
            InnerList.Add(item);
            int index = InnerList.Count - 1;
            try
            {
                OnInsertComplete(index, item);
            }
            catch
            {
                InnerList.RemoveAt(index);
                throw;
            }
        } 
        #endregion

        #region public void Clear()
        /// <summary>
        /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only. </exception>
        public void Clear()
        {
            OnClear();
            InnerList.Clear();
            OnClearComplete();
        } 
        #endregion

        #region public bool Contains(T item)
        /// <summary>
        /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> contains a specific value.
        /// </summary>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
        /// <returns>
        /// true if item is found in the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false.
        /// </returns>
        public bool Contains(T item)
        {
            return InnerList.Contains(item);
        } 
        #endregion

        #region public void CopyTo(T[] array, int arrayIndex)
        /// <summary>
        /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"></see> to an <see cref="T:System.Array"></see>, starting at a particular <see cref="T:System.Array"></see> index.
        /// </summary>
        /// <param name="array">The one-dimensional <see cref="T:System.Array"></see> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"></see>. The <see cref="T:System.Array"></see> must have zero-based indexing.</param>
        /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">arrayIndex is less than 0.</exception>
        /// <exception cref="T:System.ArgumentNullException">array is null.</exception>
        /// <exception cref="T:System.ArgumentException">array is multidimensional.-or-arrayIndex is equal to or greater than the length of array.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"></see> is greater than the available space from arrayIndex to the end of the destination array.-or-Type T cannot be cast automatically to the type of the destination array.</exception>
        public void CopyTo(T[] array, int arrayIndex)
        {
            InnerList.CopyTo(array, arrayIndex);
        } 
        #endregion

        #region public bool IsReadOnly
        /// <summary>
        /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only; otherwise, false.</returns>
        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        } 
        #endregion

        #region public bool Remove(T item)
        /// <summary>
        /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </summary>
        /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"></see>.</param>
        /// <returns>
        /// true if item was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"></see>; otherwise, false. This method also returns false if item is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"></see>.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"></see> is read-only.</exception>
        public bool Remove(T item)
        {
            OnValidate(item);
            int index = InnerList.IndexOf(item);
            if (index < 0)
                throw new ArgumentException("List does not contain item supplied");
            OnRemove(index, item);
            InnerList.RemoveAt(index);
            try
            {
                OnRemoveComplete(index, item);
                return true;
            }
            catch
            {
                InnerList.Insert(index, item);
                throw;
            }
        } 
        #endregion

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            for (int i = 0; i < InnerList.Count; i++)
            {
                yield return InnerList[i];
            }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region ICloneable Members

        object ICloneable.Clone()
        {
            return this.Clone();
        }

        public CollectionBase<T> Clone()
        {
            return new Prototype<CollectionBase<T>>(this).Clone();
        }

        #endregion
    }
}


